package capstone.tagit;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Scanner;



import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.OverlayItem;

public class Tag extends OverlayItem {
	TagType type;
	Date date;
	
	ArrayList<Attribute<String>> attributes;
	public static String delimiter = Character.toString('|');  // 219 is a Block.  Not typable on most keyboards.
//	public static String fullDelim = Character.toString((char)44) + " ";

	public Tag(GeoPoint point, String title, String snippet) {
		super(point, title, snippet);
		attributes = new ArrayList<Attribute<String>>();
	}
	
	public void setType(TagType type){
		this.type = type;
	}
	public void setDate(Date date){
		this.date=date;
	}
	public Date getDate(){
		return date;
	}
	public TagType getType(){return type;}
	
	public void addAttribute(String name, String value){
//		if (!value.equals("")){
		attributes.add(new Attribute<String>(name,value));
//			Log.v("TAG","attribute contains" + value + "that");
//		}
//		else{
//			Log.v("Tag","manufacturing attribute values");
//			attributes.add(new Attribute<String>(name,"none"));
//		}
	}
	
	public String getInfoString(SimpleDateFormat format){
		// Type|Title|Description|Latitude|Longitude|Date|Attr1|\n
		String rVal = type.getName() + delimiter + getTitle() + delimiter + getSnippet() + delimiter;
		rVal = rVal.concat(Integer.toString(mPoint.getLatitudeE6()) + delimiter +
							Integer.toString(mPoint.getLongitudeE6()) + delimiter +
							format.format(date));
		for (Attribute<String> attribute: attributes){
			rVal = rVal.concat(delimiter + attribute.getValue());
		}		
		rVal = rVal.concat(delimiter + "\n");
		
		return rVal;
	}
	
	public boolean equals(Tag t){
//		Log.v("Tag equals","checking this");
//		String s = this.mPoint.toString();
//		Log.v("Tag Equals","mPoint is " + s);
//		Log.v("Tag equals","About to do full comparison");
		return (this.date.equals(t.date)&& this.mPoint.equals(t.mPoint));
	}
	
	public static Tag parseStringToTag(String toParse, Collection<TagType> types, Collection<String> attributes, SimpleDateFormat dateFormat){
		Tag toReturn = null;
		Scanner sparse = new Scanner(toParse);
		sparse.useDelimiter("\\" + delimiter);
		while(sparse.hasNext()){
			//Tag Format
			// Type|Title|Description|Latitude|Longitude|Date|Attr1|(Optional) Attr2....
			String unparsedType = sparse.next();
//			Log.v("parsing Tag", "typename is " + unparsedType);
			String title = sparse.next();
			String description = sparse.next();
//			Log.v("parsing Tag","about to get ints");
			int latitude = sparse.nextInt();
//			Log.v("parsing Tag","got one int");
			int longitude = sparse.nextInt();
//			Log.v("parsing Tag","got ints");
			String unparsedDate = sparse.next();
//			Log.v("parsing tag","got all strings");
			Date date = new Date();//default 
			try { 
				date = dateFormat.parse(unparsedDate);//use dateformat instead
			} catch (ParseException e) {
				e.printStackTrace();
			}
			GeoPoint location = new GeoPoint(latitude,longitude);
			TagType type = TagType.findTypeByName(unparsedType,types);
			toReturn = new Tag(location,title,description);
			toReturn.setMarker(type.getMarker());
			toReturn.setType(type);
			toReturn.setDate(date);
			for (String attribute : attributes){
//				Log.v("parsting tag","in the for loop");
				String value = sparse.next();
				toReturn.addAttribute(attribute,value);
			}
		}
		
/*		StringTokenizer parse = new StringTokenizer(toParse,delimiter);
		Log.v("parsing Tag","about to enter the while loop");
		while(parse.hasMoreElements()){
			Log.v("parsing Tag","in the while loop");
			String unparsedType = parse.nextToken();
			Log.v("parsing Tag", "typename is " + unparsedType);
			String title = parse.nextToken();
			String description = parse.nextToken();
			double latitude  = Double.parseDouble(parse.nextToken());// * 1000000.0;
			double longitude = Double.parseDouble(parse.nextToken());// * 1000000.0;
			String unparsedDate = parse.nextToken();
			Date date = new Date(Date.parse(unparsedDate));
			GeoPoint location = new GeoPoint((int)latitude,(int)longitude);
			TagType type = TagType.findTypeByName(unparsedType, types);
			toReturn = new Tag(location,title,description);
			toReturn.setMarker(type.getMarker());
			toReturn.setType(type);
			toReturn.setDate(date);
			for (String attribute : attributes){
				Log.v("parsing tag","in the for loop");
				String value = parse.nextToken();
				toReturn.addAttribute(attribute, value);
				Log.v("parsing tag","end of for loop");
			}
		}*/
//		Log.v("parsing tag","returning from parse String");
		return toReturn;
	}
}